package com.lvjian.community.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lvjian.community.domain.UserProfile;
import com.lvjian.community.service.UserProfileService;
import com.lvjian.community.utils.Consts;

/**
 * 用户个人简介Controller
 */
@Controller
public class UserProfileController {

    @Autowired
    private UserProfileService userProfileService;

    // 返回用户信息
    @RequestMapping(value = "/user/profile", method = RequestMethod.POST)
    @ResponseBody
    public Object userProfile(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        JSONObject userProfileJSON = new JSONObject();
        UserProfile userProfile = userProfileService.getUserProfile(request.getParameter("username"));
        userProfileJSON.put("id", userProfile.getId());
        userProfileJSON.put("username", userProfile.getUsername());
        userProfileJSON.put("password", userProfile.getPassword());
        userProfileJSON.put("email", userProfile.getEmail());
        userProfileJSON.put("phone", userProfile.getPhone());
        userProfileJSON.put("avatar", userProfile.getAvatar());
        Integer status = userProfile.getStatus();
        String statusInfo = "";
        if (status == 0) {
            statusInfo = "未激活";
        } else if (status == 1) {
            statusInfo = "已激活";
        } else if (status == 2) {
            statusInfo = "已禁用";
        }
        userProfileJSON.put("status", statusInfo);
        userProfileJSON.put("createTime", userProfile.getCreatedAt());
        userProfileJSON.put("updateTime", userProfile.getUpdatedAt());
        userProfileJSON.put("lastLoginTime", userProfile.getLastLoginAt());
        userProfileJSON.put("description", userProfile.getRoleDescription());
        jsonObject.put(Consts.CODE, 200);
        jsonObject.put(Consts.MSG, "success");
        jsonObject.put("data", userProfileJSON);
        return jsonObject;
    }
}
